﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class OperateLogDAO :IOperateLog
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Models.OperateLogModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_OperateLog(");
            strSql.Append("Creator,VisitUrl,Description,Parameter,IpAdress,CreateDate,LogType)");
            strSql.Append(" values (");
            strSql.Append("@Creator,@VisitUrl,@Description,@Parameter,@IpAdress,@CreateDate,@LogType)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@VisitUrl", SqlDbType.VarChar,250),
					new SqlParameter("@Description", SqlDbType.NVarChar,50),
					new SqlParameter("@Parameter", SqlDbType.NVarChar,100),
					new SqlParameter("@IpAdress", SqlDbType.VarChar,20),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LogType", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.Creator;
            parameters[1].Value = model.VisitUrl;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.Parameter;
            parameters[4].Value = model.IpAdress;
            parameters[5].Value = model.CreateDate;
            parameters[6].Value = model.LogType;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_OperateLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

     
         /// <summary>
         /// 分页获取数据列表
         /// </summary>
        public List<Models.OperateLogModel> GetList(int PageSize, int PageIndex, string strWhere)
         {
             SqlParameter[] parameters = {
                     new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                     new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                     new SqlParameter("@PageSize", SqlDbType.Int),
                     new SqlParameter("@PageIndex", SqlDbType.Int),
                     new SqlParameter("@IsReCount", SqlDbType.Bit),
                     new SqlParameter("@OrderType", SqlDbType.Bit),
                     new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                     };
             parameters[0].Value = "T_OperateLog";
             parameters[1].Value = "AID";
             parameters[2].Value = PageSize;
             parameters[3].Value = PageIndex;
             parameters[4].Value = 0;
             parameters[5].Value = 1;
             parameters[6].Value = strWhere;	
             DataSet ds= SqlHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
             return ConvertToList(ds);
         }

         protected List<Models.OperateLogModel> ConvertToList(DataSet ds)
         {
             List<Models.OperateLogModel> list = new List<Models.OperateLogModel>();
             foreach (DataRow row in ds.Tables[0].Rows)
             {
                 Models.OperateLogModel model = DataRowToModel(row);
                 list.Add(model);
             }
             return list;
         }

         /// <summary>
         /// 得到一个对象实体
         /// </summary>
         public Models.OperateLogModel DataRowToModel(DataRow row)
         {
             Models.OperateLogModel model = new Models.OperateLogModel();
             if (row != null)
             {
                 if (row["AID"] != null && row["AID"].ToString() != "")
                 {
                     model.AID = int.Parse(row["AID"].ToString());
                 }
                 if (row["Creator"] != null)
                 {
                     model.Creator = row["Creator"].ToString();
                 }
                 if (row["VisitUrl"] != null)
                 {
                     model.VisitUrl = row["VisitUrl"].ToString();
                 }
                 if (row["Description"] != null)
                 {
                     model.Description = row["Description"].ToString();
                 }
                 if (row["Parameter"] != null)
                 {
                     model.Parameter = row["Parameter"].ToString();
                 }
                 if (row["IpAdress"] != null)
                 {
                     model.IpAdress = row["IpAdress"].ToString();
                 }
                 if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                 {
                     model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                 }
                 if (row["LogType"] != null)
                 {
                     model.LogType = row["LogType"].ToString();
                 }
             }
             return model;
         }
    }
}
